extends Node2D
signal load_finished
signal rushed
func _ready() -> void:
	pass
func _exit_tree() -> void:
	pass
func load_in()->void:
	var twn = create_tween()
	twn.tween_property($Display/Image,"self_modulate",Color(0.746, 0.746, 0.746, 0.196),5.0)
	twn.tween_callback(load_finished.emit)
	Global.display_str.emit("那是什么...")
	Global.display_str.emit("")
	Global.display_str.emit("天空中出现了一轮诡异的太阳")
	Global.display_str.emit("不要惊动它")
func load_out()->void:
	change_pos(0)
	ghost_close()
	if _hb_twn and _hb_twn.is_running():
		_hb_twn.kill()
	var twn = create_tween()
	twn.tween_property($Display/Image,"self_modulate",Color(0.746, 0.746, 0.746, 0.0),5.0)
	twn.tween_property($Display/Image,"scale",Vector2(0.3,0.3),5)
	twn.tween_callback(load_finished.emit)

var _hb_twn:Tween
func heartbeat()->void:
	_hb_twn=create_tween()
	for i in 5:
		_hb_twn.tween_callback($Heartbeat.play).set_delay(0.8)
	for i in 20:
		_hb_twn.tween_property($Display/Image,"scale",Vector2(0.35,0.35),0.5)
		_hb_twn.tween_callback($Heartbeat.play)
		_hb_twn.tween_property($Display/Image,"scale",Vector2(0.3,0.3),0.3)

var _current_pos_index:int = 0
func change_pos(index:int)->void:
	if index==_current_pos_index:return
	if not index in [0,1,2]:return
	_current_pos_index=index
	var twn:Tween
	twn=create_tween()
	match index:
		0:twn.tween_property($Display/Image,"position",$Marks/Marker0.position,3).set_trans(Tween.TRANS_EXPO)
		1:twn.tween_property($Display/Image,"position",$Marks/Marker1.position,3).set_trans(Tween.TRANS_EXPO)
		2:twn.tween_property($Display/Image,"position",$Marks/Marker2.position,3).set_trans(Tween.TRANS_EXPO)

func ghost()->void:
	var twn:=create_tween()
	$Display/Image/SunGhost.self_modulate=Color(1.0, 1.0, 1.0, 0.0)
	$Display/Image/SunGhost.visible=true
	$Display/Image/StrangeSunLigth.material.set_shader_parameter("sub_alpha",1.0)
	twn.tween_property($Display/Image/SunGhost,"self_modulate",Color(1.0, 1.0, 1.0, 1.0),1)
	twn.parallel().tween_property($Display/Image/StrangeSunLigth.material,"shader_parameter/sub_alpha",0.0,1.0)
	await twn.finished
	$Display/Image/StrangeSunLigth.visible=true
func ghost_close()->void:
	var twn:=create_tween()
	$Display/Image/StrangeSunLigth.material.set_shader_parameter("sub_alpha",0.0)
	twn.tween_property($Display/Image/SunGhost,"self_modulate",Color(1.0, 1.0, 1.0, 0.0),1)
	twn.parallel().tween_property($Display/Image/StrangeSunLigth.material,"shader_parameter/sub_alpha",1.0,1.0)
	await twn.finished
	$Display/Image/StrangeSunLigth.visible=false
	$Display/Image/SunGhost.visible=true
var _should_rush:bool=false
func rush()->void:
	_current_pos_index=-1
	_should_rush=true


func _process(delta: float) -> void:
	if _should_rush:
		$Display/Image.global_position=$Display/Image.global_position.move_toward(Global.player_screen_pos,delta*300)
		if ($Display/Image.global_position-Global.player_screen_pos).length()<100:
			_should_rush=false
			rushed.emit()

func _on_area_2d_area_entered(area: Area2D) -> void:
	if not area.is_in_group("player"):return
	load_out()
	rushed.emit()
